<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
        $this->display();
    }

    public function login(){
        $uname=$_POST['uname'];
        $upassword=$_POST['upassword'];

        if($uname && $upassword){
            $admin=M('admin');

            $select_data=array(
                'uname'=>$uname,
                'upassword'=>$upassword
            );

            $admin_select_res=$admin->where($select_data)->select();

            if($admin_select_res){

                session('admin_uname',$uname);

                
                $PHPSESION = cookie('PHPSESSID');
                $user_name=$admin_select_res[0]['uname'];
                
                $onlyone_login=M('onlyone_login');
                
                $select_data=array(
                    'user_name'=>$user_name
                );

                if($onlyone_login->where($select_data)->select()){
                    $update_data=array(
                        'user_name'=>$user_name,
                        'cookie'=>$PHPSESION,
                        'unitid'=>build_order_no()
                    );
                    $onlyone_login->where($select_data)->save($update_data);
                }else{
                    $add_data=array(
                        'user_name'=>$user_name,
                        'cookie'=>$PHPSESION,
                        'unitid'=>build_order_no()
                    );
                    $onlyone_login->data($add_data)->add();

                }

                $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'登录成功','return_body'=>array()));                           
            }else{
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'用户名密码错误'));            
            }
        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'参数为空'));
        }


    }

}